﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace InkRun
{
    class Tiler : GameObject
    {

        public float scale;
        public int num_tiles = 4;//0-based
        public enum TilerState
        {
            ScrollingRightToLeft,
            Stopped
        }
        public TilerState tilerState;
        int currentMainTile = 0;
        new public Vector2 velocity;
        public List<ScrollingLevel> tiles = new List<ScrollingLevel>();

        public Color color;

        public Tiler()
            : base(new Vector2(0,0))
        {
            scale = 1.0f;
            tilerState = TilerState.Stopped;
            velocity = new Vector2(-2.0f, 0.0f);
            SetUpTiler();
        }

        public void SetUpTiler()
        {
        }
        public void LoadTiles(ContentManager content)
        {
            for (int i = 0; i < num_tiles; i++)
            {
                Texture2D sprite = content.Load<Texture2D>("Sprites/Splodge_L1_S" + i);
                if (i == 0)
                {
                    tiles.Add(new ScrollingLevel(new Vector2(sprite.Bounds.Width, sprite.Bounds.Center.Y), sprite));
                }
                else
                {
                    tiles.Add(new ScrollingLevel(new Vector2(sprite.Bounds.Width * (i + 1), sprite.Bounds.Center.Y), sprite));
                }
            }
        }
        public void ResetTiler()
        {

        }
        public ScrollingLevel GetCurrentMainTile()
        {
            return tiles[currentMainTile];
        }
        public override void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            switch (tilerState)
            {
                case TilerState.ScrollingRightToLeft:
                    if (PlayerFactory.Instance().Players.Any(p => p.position.X < GameParameters.SCREEN_SIZE.X * 1 / 2) && PlayerFactory.Instance().Players.All(p => p.position.X < GameParameters.SCREEN_SIZE.X * 1 / 4))
                    {
                        tilerState = TilerState.Stopped;
                    }

                    tiles.ForEach(delegate(ScrollingLevel tile)
                    {
                        if (tile.scrollingLevelState != ScrollingLevel.ScrollingLevelState.OffScreen)
                        {
                            tile.velocity.X = -2;
                            tile.scrollingLevelState = ScrollingLevel.ScrollingLevelState.ScrollingRightToLeft;
                            tile.Update(gameTime);
                        }
                        else
                        {                       
                            tiles.RemoveAt(currentMainTile);
                        }
                    });
                    break;
                case TilerState.Stopped:
                    if (PlayerFactory.Instance().Players.Any(p => p.position.X > GameParameters.SCREEN_SIZE.X * 1 / 2) && PlayerFactory.Instance().Players.All(p => p.position.X > GameParameters.SCREEN_SIZE.X * 1 / 4))
                    {
                        tilerState = TilerState.ScrollingRightToLeft;
                    }
                    tiles.ForEach(delegate(ScrollingLevel tile)
                    {
                        if (tile.scrollingLevelState != ScrollingLevel.ScrollingLevelState.OffScreen)
                        {
                            tile.scrollingLevelState = ScrollingLevel.ScrollingLevelState.Stopped;
                            tile.Update(gameTime);
                        }
                    });
                    break;
                default:
                    break;
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (ScrollingLevel sl in tiles)
            {
                sl.Draw(gameTime, spriteBatch);
            }
        }

        public override void Die()
        {
            base.Die();
        }
    }
}
